package com.nsgf.library.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.WaterRecordsBean;
import com.nsgf.library.utils.UnitTo;

import java.util.ArrayList;
import java.util.List;

/**
 * Custom implementation of the MarkerView.
 *
 * @author Philipp Jahoda
 */
@SuppressLint("ViewConstructor")
public class MyMarkerView extends MarkerView {

    private TextView tvDate;
    private TextView tvValue;
    private TextView tv_title;
    private View view_color;
    private ValueFormatter xAxisValueFormatter;
    private ValueFormatter yAxisValueFormatter;
    private String title = "", unit = "#3CC3DB";
    private List<WaterRecordsBean.WaterRecordsListBean> list = new ArrayList<>();

    public MyMarkerView(Context context,
                        ValueFormatter xAxisValueFormatter,
                        ValueFormatter yAxisValueFormatter,
                        String title,
                        String unit) {
        super(context, R.layout.custom_marker_view);
        this.xAxisValueFormatter = xAxisValueFormatter;
        this.yAxisValueFormatter = yAxisValueFormatter;

        tvDate = findViewById(R.id.tv_date);
        tvValue = findViewById(R.id.tv_value);
        view_color = findViewById(R.id.view_color);
        tv_title = findViewById(R.id.tv_title);
        this.title = title;
        this.unit = unit;
    }

    public MyMarkerView(Context context,
                        ValueFormatter xAxisValueFormatter,
                        ValueFormatter yAxisValueFormatter,
                        String title,
                        String unit,
                        List<WaterRecordsBean.WaterRecordsListBean> list) {
        super(context, R.layout.custom_marker_view);
        this.xAxisValueFormatter = xAxisValueFormatter;
        this.yAxisValueFormatter = yAxisValueFormatter;

        tvDate = findViewById(R.id.tv_date);
        tvValue = findViewById(R.id.tv_value);
        tv_title = findViewById(R.id.tv_title);
        view_color = findViewById(R.id.view_color);
        this.title = title;
        this.unit = unit;
        this.list = list;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        //展示自定义X轴值 后的X轴内容
        try {
            tvDate.setText(list.get((int) e.getX()).getTm());
        } catch (Exception ex) {
            ex.getMessage();
            tvDate.setText(xAxisValueFormatter.getAxisLabel(e.getX(), null));
        }
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.OVAL);
        drawable.setColor(Color.parseColor(unit));
        view_color.setBackground(drawable);
        tvValue.setText(String.valueOf(e.getY()));
        tv_title.setText(title);
        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}
